
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  importPool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.StringBuffer;

import logger.PoolLogger;
import dbmanager.DBManager;


public class SortBuild 
{
	String input_file;
	PoolLogger pl;
	
	public SortBuild(String input_file) 
		{
			this.input_file =input_file;
			
			// logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
		}
//---------------------read input file & return file as string buffer	
	public StringBuffer read_file()
		{	
			try 
			{
				RandomAccessFile file = new RandomAccessFile((new File(input_file)),"r");
				StringBuffer File = new StringBuffer() ;
				String line;
				int i=0;
				while((line=file.readLine())!=null)
				{
					File.append(line+"\n");
					i++;
				}
				file.close();
				return File;
			}
			catch (FileNotFoundException e) 
			{
				System.out.println("FilenotfoundException in ->Read File");
				pl.info("FilenotfoundException in ->Read File");
				pl.debug("FilenotfoundException in ->Read File");
				pl.warn("FilenotfoundException in ->Read File");
				pl.fatal("FilenotfoundException in ->Read File");
				pl.error("FilenotfoundException in ->Read File");
				pl.trace("FilenotfoundException in ->Read File");
				
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				System.out.println("IOException in ->Read File");
				pl.info("IOException in ->Read File");
				pl.debug("IOException in ->Read File");
				pl.warn("IOException in ->Read File");
				pl.fatal("IOException in ->Read File");
				pl.error("IOException in ->Read File");
				pl.trace("IOException in ->Read File");
				
				e.printStackTrace();
				pl.info(e.getMessage());
				pl.debug(e.getMessage());
				pl.warn(e.getMessage());
				pl.fatal(e.getMessage());
				pl.error(e.getMessage());
				pl.trace(e.getMessage());
			}
			return null;
		}
	//-------------------sort String buffer according to y 
	public StringBuffer[] Sort(StringBuffer line[])
	{
		for(int i=0;i<line.length;i++)
		{
			String SubString[] = line[i].toString().split("\t");
			int y = Integer.parseInt(SubString[2]);
			int min =y;
			for(int j=i+1;j<line.length;j++)
			{
				String Sub[] = line[j].toString().split("\t");
				y = Integer.parseInt(Sub[2]);
				if(min>y)
				{
					 StringBuffer temp = line[i];
					 line[i] = line[j];
					 line[j] =  temp;
					 min = y; 
				}
			}
		}
		return line;
	}
	//--------------sort string buffer according to x if y is same----------------
	public StringBuffer[] Sort_x(StringBuffer line[])
	{
		for(int i=0;i<line.length;i++)
		{
			String SubString[] = line[i].toString().split("\t");
			int y = Integer.parseInt(SubString[2]);
			int x = Integer.parseInt(SubString[1]);
			int pre_y =y;
			int min=x;
			for(int j=i+1;j<line.length;j++)
			{
				String Sub[] = line[j].toString().split("\t");
				y = Integer.parseInt(Sub[2]);
				x = Integer.parseInt(Sub[1]);
				if(pre_y==y)
				if(min>x)
				{
					 StringBuffer temp = line[i];
					 line[i] = line[j];
					 line[j] =  temp;
					 min = x; 
				}
			}
		}
		return line;
	}
	//-----------------convert string array into string buffer array
	public StringBuffer[] Convert_String(String sort[])
	{
		StringBuffer temp[] = new StringBuffer[sort.length]; 
		for(int i=0;i<sort.length;i++)
		{
			temp[i] = new StringBuffer(sort[i]);
		}
		return temp;
	}
	
	public static void main(String[] args) 
	{
		SortBuild sb = new SortBuild("/home/wiki/Desktop/ItemList/check.txt");		
		StringBuffer temp1[] =sb.sortfile();
		for(int i=0;i<temp1.length;i++)
		{
			System.out.println("sorted file->"+temp1[i]);
		}
	}
	//------------------return sorted String buffer --------------------
	public StringBuffer[] sortfile()
	{
		return Sort_x( Sort(Convert_String(read_file().toString().split("\n"))));
	}
}
